﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EnvironmentManagerSystem.BLL;
using EnvironmentManagerSystem.MODE;
namespace EnvironmentManagerSystem.DepartmentManagement
{
    public partial class DepartmentManagement : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return;if (!IsPostBack)
            {
                if (Convert.ToBoolean(Session["Admin"]) != true)
                {
                    Button1.Visible = false;
                    Button2.Visible = false;
                    btnReBind.Visible = false;
                    gvDepartment.Columns[2].Visible = false;
                }
                    BindGridView();
            }
        }

        private void BindGridView()
        {

            gvDepartment.DataSource = ExecutiveDepartmentService.GetExecutiveDepartmentAll();
            gvDepartment.DataBind();
        }

        protected void gvDepartment_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDepartment.EditIndex = e.NewEditIndex;
            BindGridView();
        }

        protected void gvDepartment_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ed_Id = (int)gvDepartment.DataKeys[e.RowIndex].Value;
            if (!ExecutiveDepartmentService.DeleteExecutiveDepartment(ed_Id))
                Jscript.Alert("删除失败！", this);
            BindGridView();
        }

        protected void gvDepartment_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            ExecutiveDepartmentModels ed = new ExecutiveDepartmentModels();
            ed.Ed_Id = Convert.ToInt32(gvDepartment.DataKeys[e.RowIndex].Value);
            ed.Ed_Name = ((TextBox)gvDepartment.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim();
            if (!ExecutiveDepartmentService.UpdateExecutiveDepartment(ed))
                Jscript.Alert("修改失败！", this);
            gvDepartment.EditIndex = -1;
            BindGridView();
        }

        protected void gvDepartment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDepartment.EditIndex = -1;
            BindGridView();
        }

        protected void gvDepartment_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDepartment.PageIndex = e.NewPageIndex;
        }

        protected void gvDepartment_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void btnReBind_Click(object sender, EventArgs e)
        {
            BindGridView();
        }
    }
}
